package de.fuberlin.inf.alp3.ue09;

import java.util.Stack;

class CircleSearch implements Node.Action {
    public static Stack<Node> path = new Stack<Node>();
    public static Node initial;
    private String showpath() {
        StringBuffer sb = new StringBuffer();
        sb.append('[');
        if (path.size() == 0)
            sb.append(']');
        else {
            for (Node n : path)
                sb.append(((Graph) n).name + ",");
            sb.setCharAt((sb.length()-1), ']');
        }
        return sb.toString();
    }
    public boolean act(Node n) {
        if (path.size() > 1 && path.get(path.size()-2).equals(n)) {
            return false;
        }
        if (!n.visited()) {
            System.out.print(((Graph) n).name);
            System.out.println("  " + showpath());
            path.push(n);
            n.setVisited(true);
            boolean found = n.iterate(this);
            n.setVisited(false);
            if (!found)
                path.pop();
            else {
                path.pop();
                return true;
            }
        }
        else {
            System.out.println(((Graph) n).name + " (markiert)");
            if (n.equals(CircleSearch.initial))
                return true;
        }
        return false;
    }
}